//
//  CustomNavViewController.m
//  Yunho2
//
//  Created by l on 13-6-4.
//
//

#import "CustomNavViewController.h"

@interface CustomNavViewController ()

@end

@implementation CustomNavViewController
@synthesize flagArray;
@synthesize tabbarViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    if (self = [super initWithRootViewController:rootViewController]) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.flagArray = [NSMutableArray arrayWithCapacity:0];
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated tabbarHidden:(BOOL)hidden
{
    //[super pushViewController:viewController animated:animated];
    //CustomTabbarViewController *tabbarViewController = (CustomTabbarViewController *)self.tabBarController;
    [self.tabbarViewController hideTabBar:hidden];
    [self.flagArray addObject:[NSNumber numberWithBool:hidden]];
    [super pushViewController:viewController animated:animated];
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //[super pushViewController:viewController animated:animated];
    //CustomTabbarViewController *tabbarViewController = (CustomTabbarViewController *)self.tabBarController;
    
    [self.tabbarViewController hideTabBar:YES];
//    [self.flagArray addObject:[NSNumber numberWithBool:YES]];
    [super pushViewController:viewController animated:animated];
}

-(void)popToRootViewControllerAnimated:(BOOL)animated
{
    //[super popToRootViewControllerAnimated:animated];
    //CustomTabbarViewController *tabbarViewController = (CustomTabbarViewController *)self.tabBarController;
    [self.tabbarViewController hideTabBar:NO];
//    [self.flagArray removeAllObjects];
    [super popToRootViewControllerAnimated:animated];
}

-(void)popViewControllerAnimated:(BOOL)animated
{
    //CustomTabbarViewController *tabbarViewController = (CustomTabbarViewController *)self.tabBarController;
//    if (self.flagArray.count == 1) {
//        [self.tabbarViewController hideTabBar:NO];
//    }else if (self.flagArray.count > 1){
//        NSNumber *tempNum = [self.flagArray objectAtIndex:self.flagArray.count-2];
//        BOOL hidden = [tempNum boolValue];
//        [self.tabbarViewController hideTabBar:hidden];
//    }
//    [self.flagArray removeLastObject];
    if (self.viewControllers.count == 1) {
        [self.tabbarViewController hideTabBar:NO];
    }
    [super popViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    for (NSInteger i = self.viewControllers.count - 1; i > 0; i--) {
        if (viewController == self.viewControllers[i]) {
            break;
        }
        [self.flagArray removeLastObject];
    }
    return [super popToViewController:viewController animated:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
